package dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import bean.Employee;

public class CreateEmployeeDAO extends DAO<Employee> {
	public CreateEmployeeDAO(SessionFactory factory) {
		super(factory);
	}
	
	public void createEmployee(Employee employee) {
		Transaction tx = null;
		Session session = null;
		try {				
			session = this.getSession();
			tx = session.beginTransaction();
			session.save(employee);
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			if(session != null)	session.close();
		}
	}
	
	public Employee lookUpByUsername(String name) {
		List<Employee> list = this.getByAttribute("username", name, Employee.class);
		if (list != null && list.size() > 0) {
			Employee employee = list.get(0);
			if(employee.getUsername().equals(name))
				return employee;
		}
		return null;
	}
}
